package com.ggh.jinjilive.ui.mine.family;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityRankingListBinding;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.tencent.qcloud.tim.uikit.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;


@Route(path = ARouterUtils.RankingListActivity)
public class RankingListActivity extends BaseActivity<ActivityRankingListBinding> implements View.OnClickListener {

    private String[] mFragmentTitles = {"本周排行", "上周排行"};
    private int[] mFragmentStatus = {1, 2};

    private List<RankingListFragment> rankingListFragmentList = new ArrayList<>();
    //适配器
    private ProfitOrderAdapter profitOrderAdapter;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).init();
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_ranking_list;
    }


    @Override
    public void initView() {
        super.initView();
        //获取状态栏高度
        binding.llSpacing.setPadding(0, ScreenUtil.getStatusBarHeight(),0,0);

    }

    @Override
    public void initData() {
        super.initData();
        for (int i = 0; i < mFragmentTitles.length; i++) {
            RankingListFragment rankingListFragment = new RankingListFragment(mFragmentStatus[i]);
            rankingListFragmentList.add(rankingListFragment);
        }
        profitOrderAdapter = new ProfitOrderAdapter(getSupportFragmentManager());
        binding.orderVp.setAdapter(profitOrderAdapter);
        binding.orderVp.setOffscreenPageLimit(mFragmentTitles.length);
        binding.stlOrder.setViewPager(binding.orderVp, mFragmentTitles);
        binding.orderVp.setCurrentItem(0);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //软键盘搜索
        binding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if(null != profitOrderAdapter && null != profitOrderAdapter.rankingListFragment){
                        profitOrderAdapter.rankingListFragment.setSearch(v.getText().toString());
                    }
                    hideSoftInput();
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    class ProfitOrderAdapter extends FragmentPagerAdapter {
        public RankingListFragment rankingListFragment;

        public ProfitOrderAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public RankingListFragment getItem(int position) {
            return rankingListFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return rankingListFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles[position];
        }

        @Override
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            rankingListFragment = (RankingListFragment) object;
            super.setPrimaryItem(container, position, object);
        }
    }
}
